module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  this.gameChanel = this.channelService.getChannel("GameChannel",true)
}

var handler = Handler.prototype


//获取闯关数据
handler.getCheckpointInfo = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.hunanRunQuickly.getCheckpointInfo(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//获取闯关排行榜
handler.getCheckpointRanking = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.hunanRunQuickly.getCheckpointRanking(null,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//获取闯关历史记录
handler.getCheckpointHistory = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.hunanRunQuickly.getCheckpointHistory(null,msg.dayStr,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}

//获取房间战绩
handler.getHistory = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.hunanRunQuickly.getHistory(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//领取闯关红包
handler.getCheckpointRed = function(msg,session,next) {
	var uid = session.get("uid")
	var checkpoint = msg.checkpoint
	if(!uid || !checkpoint){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.hunanRunQuickly.getCheckpointRed(null,uid,checkpoint,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//领取分享关卡红包
handler.getShareCheckpointRed = function(msg,session,next) {
	var uid = session.get("uid")
	var checkpoint = msg.checkpoint
	var itemId = msg.itemId
	var value = msg.value
	var isApp = msg.isApp
	if(!uid || !checkpoint || !itemId || !value){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.hunanRunQuickly.getShareCheckpointRed(null,uid,checkpoint,itemId,value,isApp,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}